<?php
class Core_Data_Mappers_Trainingexpertise extends Core_Data_Mappers_Abstract {
	
	protected $_businessclass="Core_Business_Trainingexpertise";
	protected $_modelclass="Core_Data_Models_Trainingexpertise";
	protected $_map=array("Trainingid"=>"trainingid","Expertiseid"=>"expertiseid");
	
	public function find($id,$expertise=-1) {
		if($expertise==-1) {$filter = array("trainingid = ?"=>$id);}
		else {$filter = array("trainingid = ?"=>$id,"expertiseid = ?"=>$expertise);}
		$row = $this->getDbTable()->fetchAll($filter);
		if (isset($row) && count($row)>0) {
			if (count($row)==1) {
				$o = $this->map($row->current());
			}
			else  {
				$o=array();
				foreach ($row as $item) 
					$o[]=$this->map($item);
				
			}
			return $o;
		}
		return null;
	}
	
	public function add($object) {
		if(!$this->exists($object)) {			
			$data = $this->reversemap($object);
			$this->getDbTable()->insert($data);
		}
	}
	
	public function edit($object) {
		if($this->exists($object)) {
			$data = $this->reversemap($object);
			unset($data['trainingid']);
			$this->getDbTable()->update($data, array("trainingid = ?"=>$object->Trainingid));
		}
	}
	
	public function delete($object) {
		if($this->exists($object)) {
			$where = array("trainingid = ?"=>$object->Trainingid,"expertiseid = ?"=>$object->Expertiseid);
			$this->getDbTable()->delete($where);
		}
	}
	public function flushCategory($trainingid) {
		$where = array("trainingid = ?"=>$trainingid);
		$this->getDbTable()->delete($where);
	}
	
	public function exists($object) {
		if($object instanceof $this->_businessclass && null!==$this->find($object->Trainingid,$object->Expertiseid))
			return true;
		else 
			return false;
	}
	
	public function fetchAll($where = null,$count=10,$start=0) {
		$rows = $this->getDbTable()->fetchAll($where,null,$count,$start);
		if(isset($rows)&& count($rows)>0) {
			$Collections = array();
			foreach ($rows as $item) {
				$Collections[] = $this->map($item);
			}
			return $Collections;
		}
		return null;
	}
}
?>